/*
 * BanBu_EditTextViewController
 * @description 本文件提供对话收藏中的草稿界面的ViewController
 * @package
 * @author 		***
 * @copyright
 * @version 		1.0
 * @description 本文件提供对话收藏中的草稿界面的ViewController
 */

#import "BanBu_EditTextViewController.h"
#import "BanBu_NavButton.h"
#import "BanBu_ThemeManager.h"
#import "BanBuAPIs.h"
#import "BanBu_ManuscriptAPI.h"
#import "BanBu_SmileViewAPI.h"
#import "AppDataManager.h"
#import "AppCommunicationManager.h"
#import "BanBu_ImageView.h"
#import "BanBu_ChatViewController.h"

@interface BanBu_EditTextViewController ()

@end

@implementation BanBu_EditTextViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.title = NSLocalizedString(@"manuscriptTitle", nil);
    _inputText.text = _inputString;
    [_inputText becomeFirstResponder];
    
    _inputText.layer.borderWidth = 1.f;
    _inputText.layer.borderColor = [UIColor grayColor].CGColor;
    _inputText.layer.cornerRadius = 8;
    
    [self createFinishButton];
    [self createCancelButton];
    
    [_inputText setFrame:CGRectMake(10, 3, 300, __MainScreen_Height - 348)];
    [_sendButton setFrame:CGRectMake(60, _inputText.frame.size.height + 14, 200, 35)];
    BanBu_ImageView *img = [[[BanBu_ImageView alloc] initWithBackGroundImageType:UIImageViewCreatTypeTable] autorelease];
    self.view.backgroundColor = [UIColor colorWithPatternImage:img.image];
    
    //收藏成功后返回上一界面
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(popToPrevious) name:KFinishCollecttingAmsg object:nil];
    
    [_sendButton setBackgroundImage:[UIImage imageNamed:@"btn_emo_download_bg.png"] forState:UIControlStateNormal];
    [_sendButton setTitle:NSLocalizedString(@"sendReply", nil) forState:UIControlStateNormal];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc
{
    [_inputText release];
    [_sendButton release];
    [super dealloc];
}

-(void)createFinishButton
{
    //添加“完成”按钮
    finishButton = [[BanBu_NavButton alloc] initWithButtonType:UIButtonCreatTypeNormal];
    finishButton.position = ButtonPositionRight;
    CGFloat btnLen1 = [NSLocalizedString(@"finishButton", nil) sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(150, 30)].width;
    finishButton.frame=CGRectMake(320-btnLen1-20-5, 7, btnLen1+20, 30);
    [finishButton setTitle:NSLocalizedString(@"finishButton", nil) forState:UIControlStateNormal];
    
    finishButton.tag = 101;
    [finishButton addTarget:self action:@selector(finishEditting) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *delItem = [[[UIBarButtonItem alloc] initWithCustomView:finishButton] autorelease];
    self.navigationItem.rightBarButtonItem = delItem;
}

//取消按钮
-(void)createCancelButton
{
    //添加“取消”按钮
    cancelButton = [[BanBu_NavButton alloc] initWithButtonType:UIButtonCreatTypeNormal];
    cancelButton.position = ButtonPositionRight;
    CGFloat btnLen1 = [NSLocalizedString(@"cancelNotice", nil) sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(150, 30)].width;
    cancelButton.frame=CGRectMake(320-btnLen1-20-5, 7, btnLen1+30, 30);
    [cancelButton setTitle:NSLocalizedString(@"cancelNotice", nil) forState:UIControlStateNormal];
    
    
    cancelButton.tag = 101;
    [cancelButton addTarget:self action:@selector(cancelEditting) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *delItem = [[[UIBarButtonItem alloc] initWithCustomView:cancelButton] autorelease];
    self.navigationItem.leftBarButtonItem = delItem;
}

#pragma mark - 编辑、取消、完成
//编辑完成
-(void)finishEditting
{
    if ([[_inputText.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] isEqualToString:@""] )
    {
        [TKLoadingView showTkloadingAddedTo:self.navigationController.view title:NSLocalizedString(@"kongAlert", nil) activityAnimated:NO duration:1.3f];
        return;
    }

    //2014年09月23日14:59:31 by yinlinlin，提交一次之后，不能再点，防止数据重复
    finishButton.userInteractionEnabled = NO;
    //添加收藏的内容
    NSMutableDictionary * parDic = [NSMutableDictionary dictionaryWithCapacity:8];
    [parDic setValue:_inputText.text forKey:KeyContent];
    [parDic setValue:[NSNumber numberWithInt:MsgTypeText] forKey:KeyType];
    [parDic setValue:MyAppDataManager.useruid forKey:KeyFromUidTalk];
    if (_manuscriptDic)
    {
        [parDic setValue:[self.manuscriptDic valueForKey:KeyFavid] forKey:KeyFavid];
    }
    
    NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSString * collectedTime = [formatter stringFromDate:[NSDate date]];
    
    [parDic setValue:collectedTime forKey:KeyCollectAmsgTime];
    
    NSLog(@"parDic:%@",parDic);
    //提交服务器
    [AppComManager getBanBuDataWIthCollectEmo:BanBu_AddCollectAmsg_ByUser par:parDic delegate:MyAppDataManager];
}

//取消编辑
-(void)cancelEditting
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)popToPrevious
{
    [self.navigationController popViewControllerAnimated:YES];
}

//发送
- (IBAction)sendMsg:(UIButton *)sender
{
    if ([[_inputText.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] isEqualToString:@""] )
    {
        [TKLoadingView showTkloadingAddedTo:self.navigationController.view title:NSLocalizedString(@"kongAlert", nil) activityAnimated:NO duration:1.3f];
        return;
    }
    
    NSMutableDictionary * sendDic = [NSMutableDictionary dictionaryWithCapacity:8];
    //修改：yinlinlin:2014/07/03
    [sendDic setValue:_inputText.text forKey:KeyContent];
//    [sendDic setValue:[_inputText.text valueForKey:KeyContent] forKey:KeyContent];
    [sendDic setValue:[NSNumber numberWithInt:ChatCellTypeText] forKey:KeyType];
    [sendDic setValue:@"mo" forKey:KeyShowFrom];
    
    //发送消息
    [[NSNotificationCenter defaultCenter] postNotificationName:kSendMsgAllTypeNotification object:sendDic];
    
    //popToChat
    [self.navigationController popToViewController:_appChatController animated:YES];
}
@end
